<!--
 * @Author: HeAo
 * @Date: 2021-10-12 14:35:38
 * @LastEditTime: 2021-10-12 15:00:26
 * @LastEditors: HeAo
 * @Description: 
 * @FilePath: \web2103-2105\web2103code\3-2-Vue\day04\3-插槽应用.html
 * 别乱动！
-->
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <script src="../js/vue.js"></script>
</head>

<body>
  <div id="app">
    <my-table :data="students">
      <template #thead>
        <th>编号</th>
        <th>姓名</th>
        <th>年龄</th>
      </template>
      <template #td="{item}">
        <td>{{item.id}}</td>
        <td>{{item.name}}</td>
        <td>{{item.age}}</td>
      </template>
    </my-table>
    <!-- 教师表格生成 -->
    <my-table :data="teachers">
      <template #thead>
        <th>编号</th>
        <th>姓名</th>
        <th>薪资</th>
        <th>性别</th>
      </template>
      <template #td="{item}">
        <td>{{item.id}}</td>
        <td>{{item.name}}</td>
        <td>{{item.salary}}</td>
        <td>{{item.gender}}</td>
      </template>
    </my-table>
  </div>
  <script>
    let component1 = {
      props: {
        data: {
          type: Array,
          required: true
        }
      },
      data() {
        return {}
      },
      template: `
        <div>
          <table>
            <thead>
              <tr>
                <slot name="thead"></slot>  
              </tr>  
            </thead>
            <tbody>
                <tr v-for="(item, index) in data" :key="index">
                  <slot name="td" :item="item"></slot>
                </tr>
            </tbody>  
          </table>  
        </div>
      `
    }
    let vm = new Vue({
      el: '#app',
      data: {
        students: [
          { id: 1, name: '张三', age: 12 },
          { id: 2, name: '李四', age: 15 },
          { id: 3, name: '王五', age: 16 },
          { id: 4, name: '路人甲', age: 13 },
        ],
        teachers: [
          { id: 1, name: '张老师', salary: 5000, gender: 'male' },
          { id: 2, name: '李老师', salary: 6000, gender: 'female' },
          { id: 3, name: '王老师', salary: 7000, gender: 'female' },
          { id: 4, name: '罗老师', salary: 8000, gender: 'male' },
        ]
      },
      methods: {},
      created() { },
      components: {
        myTable: component1
      }
    })
  </script>
</body>

</html>